package com.nkdx.ws.service.impl;

import com.nkdx.ws.constant.CommonConstant;
import com.nkdx.ws.dto.MeteorologicalDataDto;
import com.nkdx.ws.dto.RegionDto;
import com.nkdx.ws.dto.UserDto;
import com.nkdx.ws.enums.CodeEnum;
import com.nkdx.ws.eto.MeteorologicalDataEto;
import com.nkdx.ws.eto.UserEto;
import com.nkdx.ws.exception.DataException;
import com.nkdx.ws.mapper.MeteorologicalDataEtoMapper;
import com.nkdx.ws.mapper.UserMapper;
import com.nkdx.ws.service.MeteorologicalDataService;
import com.nkdx.ws.service.RegionService;
import com.nkdx.ws.service.UserService;
import com.nkdx.ws.util.Utils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 *
 *  天气数据 逻辑层
 * @author ZZW
 * @date 2022/3/6 3:37 下午
 * @param
 * @return
 */
@Service
public class MeteorologicalDataServiceImpl implements MeteorologicalDataService {

    private MeteorologicalDataEtoMapper meteorologicalDataEtoMapper;
    private HttpServletRequest request;
    private RegionService regionService;
    private UserMapper userMapper;
    @Autowired
    public MeteorologicalDataServiceImpl (
            MeteorologicalDataEtoMapper meteorologicalDataEtoMapper,
            HttpServletRequest request,
            RegionService regionService,
            UserMapper userMapper
    ){
        this.meteorologicalDataEtoMapper = meteorologicalDataEtoMapper;
        this.request = request;
        this.regionService = regionService;
        this.userMapper = userMapper;
    }


    @Override
    public MeteorologicalDataEto insert(MeteorologicalDataEto eto) {

        if (eto.getCityId() == null ||
                StringUtils.isAnyBlank(eto.getWeather(),eto.getAirTemperature(),eto.getPressure(),
                        eto.getWindPower(),eto.getWindDirection(),eto.getHumidity())){
            throw new DataException(CodeEnum.COM_MISS_PARAM_ERROR);
        }
            Date date = new Date();
            eto.setCreateTime(date);
            eto.setUpdateTime(date);
            UserDto user = (UserDto) request.getSession().getAttribute("user");
            if (user == null){
                throw new DataException(CodeEnum.ERROR_APP_ID_NOT_EXISTS);
            }

            eto.setCreateUserId(user.getId());
            eto.setUpdateUserId(user.getId());
            eto.setStatus(CommonConstant.STATUS_SUCCESS);
            meteorologicalDataEtoMapper.insert(eto);
        return eto;
    }

    @Override
    public MeteorologicalDataEto release(MeteorologicalDataEto eto) {
        String name = request.getHeader("name");
        String pwd = request.getHeader("pwd");

        if (eto.getCityId() == null ||
                StringUtils.isAnyBlank(eto.getWeather(),eto.getAirTemperature(),eto.getPressure(),
                        eto.getWindPower(),eto.getWindDirection(),eto.getHumidity(),name,pwd)){
            throw new DataException(CodeEnum.COM_MISS_PARAM_ERROR);
        }
        Date date = new Date();
        eto.setCreateTime(date);
        eto.setUpdateTime(date);
        UserDto user = userMapper.findUserByName(name);
        if (user == null){
            throw new DataException(CodeEnum.ERROR_APP_ID_NOT_EXISTS);
        }
        if (!Utils.MD5(pwd).equalsIgnoreCase(user.getPassword())){
            throw new DataException(CodeEnum.ERROR_NAME_PWD_ERROR);
        }
        eto.setCreateUserId(user.getId());
        eto.setUpdateUserId(user.getId());
        eto.setStatus(CommonConstant.STATUS_SUCCESS);
        meteorologicalDataEtoMapper.insert(eto);
        return eto;
    }

    @Override
    public MeteorologicalDataEto findById(Integer id) {
        if (id != null){
            return meteorologicalDataEtoMapper.selectByPrimaryKey(id);
        }
        return null;
    }

    @Override
    public MeteorologicalDataEto update(MeteorologicalDataEto eto) {
        if (eto.getId() == null || eto.getCityId() == null ||
                StringUtils.isAnyBlank(eto.getWeather(),eto.getAirTemperature(),eto.getPressure(),
                        eto.getWindPower(),eto.getWindDirection(),eto.getHumidity())){
            throw new DataException(CodeEnum.COM_MISS_PARAM_ERROR);
        }
        meteorologicalDataEtoMapper.updateByPrimaryKeySelective(eto);
        return eto;
    }

    @Override
    public List<MeteorologicalDataDto> findAll() {

        return meteorologicalDataEtoMapper.findAll();
    }

    @Override
    public void invalidById(Integer id) {
        if (id != null){
            MeteorologicalDataEto updateEto = new MeteorologicalDataEto();
            updateEto.setId(id);
            updateEto.setStatus(CommonConstant.STATUS_fail);
            updateEto.setUpdateTime(new Date());
            UserDto user = (UserDto) request.getSession().getAttribute("user");
            if (user == null){
                throw new DataException(CodeEnum.ERROR_APP_ID_NOT_EXISTS);
            }
            updateEto.setUpdateUserId(user.getId());
            meteorologicalDataEtoMapper.updateByPrimaryKeySelective(updateEto);
        }else {
            throw new DataException(CodeEnum.COM_MISS_PARAM_ERROR);
        }
    }

    @Override
    public List<MeteorologicalDataDto> findAllByCity() {
        String city = request.getParameter("city");
        String cityIdStr = request.getParameter("cityId");
        Integer cityId = null;
        if (StringUtils.isNotBlank(city)){

            if (city.equals("local")){

            }else {
                RegionDto dto = regionService.findCityByLikeName(city);
                if (dto != null){
                    cityId = dto.getId();
                }
            }
        }
        if (StringUtils.isNotBlank(cityIdStr)){
            try {
                cityId = Integer.valueOf(cityIdStr);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if (cityId == null){
            cityId = CommonConstant.CITY_ID_BEIJING;
        }
        return meteorologicalDataEtoMapper.findAllByCity(cityId, CommonConstant.STATUS_SUCCESS);
    }
}
